﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;

public partial class Templates_Office_Default : BaseTemplatePage
{
    protected override void AttachEvents()
    {
        acmsAddress.DataBound += new EventHandler(acmsAddress_DataBound);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    void acmsAddress_DataBound(object sender, EventArgs e)
    {
        if (acmsAddress.Text != null && acmsAddress.Text.Trim().EndsWith(","))
        {
            acmsAddress.Text = acmsAddress.Text.Trim().Remove(acmsAddress.Text.Trim().Length - 1);
        }
    }

    public override void DataBind()
    {
        if (AcmsContext.CurrentPost != null)
        {
            PostControl addressControl = AcmsContext.CurrentPost.FindPostControl("Address");
            if (addressControl != null)
            {
                TextContentControlProperties prop = addressControl.GetProperties<TextContentControlProperties>();
                if (prop != null && !string.IsNullOrEmpty(prop.Text))
                {
                    map.Address = prop.Text;
                }
            }

            PostControl townControl = AcmsContext.CurrentPost.FindPostControl("Town");
            if (addressControl != null)
            {
                TextContentControlProperties prop = townControl.GetProperties<TextContentControlProperties>();
                if (prop != null && !string.IsNullOrEmpty(prop.Text))
                {
                    map.Address = string.Concat(map.Address, ", ", prop.Text).Trim();
                }
            }

            PostControl postCodeControl = AcmsContext.CurrentPost.FindPostControl("PostCode");
            if (postCodeControl != null)
            {
                TextContentControlProperties prop = postCodeControl.GetProperties<TextContentControlProperties>();
                if (prop != null && !string.IsNullOrEmpty(prop.Text))
                {
                    map.Address = string.Concat(map.Address, " ", prop.Text).Trim();
                }
            }

            PostControl emailControl = AcmsContext.CurrentPost.FindPostControl("Email");
            if (emailControl != null)
            {
                TextContentControlProperties prop = emailControl.GetProperties<TextContentControlProperties>();
                if (prop != null && !string.IsNullOrEmpty(prop.Text))
                {
                    lnkEmail.NavigateUrl = string.Concat("mailto:", prop.Text.Trim());
                    lnkEmail.Text = prop.Text.Trim();
                }
            }
            PostControl latatudeControl = AcmsContext.CurrentPost.FindPostControl("Latitude");
            if (latatudeControl != null)
            {
                TextContentControlProperties prop = latatudeControl.GetProperties<TextContentControlProperties>();
                if (prop != null && !string.IsNullOrEmpty(prop.Text))
                {
                    try
                    {
                        map.Latitude = float.Parse(prop.Text.Trim());
                    }
                    catch
                    {
                    }
                }
            }
            PostControl longitudeControl = AcmsContext.CurrentPost.FindPostControl("Longitude");
            if (longitudeControl != null)
            {
                TextContentControlProperties prop = longitudeControl.GetProperties<TextContentControlProperties>();
                if (prop != null && !string.IsNullOrEmpty(prop.Text))
                {
                    try
                    {
                        map.Longitude = float.Parse(prop.Text.Trim());
                    }
                    catch
                    {
                    }
                }
            }
        }
        else
        {
            map.Address = "Flat B, 8 Ashmore Road, London";
            lnkEmail.Text = "Email Here";
        }
        base.DataBind();
    }

}
